const express = require('express')
const addOrder = express.Router()
const db = require('../../mysql/db')

function query(data) {
    data.orderState = String(data.orderState)
    return new Promise((resolve, reject) => {
        db.query(
            `            
    INSERT INTO orders SET 
    orderID = ?,user = ?, phone = ?, address = ?, commodityName = ?, commodityNum = ?, 
    commodityPrice = ?,orderState = ?
    `,
            [
                data.orderID,
                data.user,
                data.phone,
                data.address,
                data.commodityName,
                data.commodityNum,
                data.commodityPrice,
                data.orderState
            ], (err, results) => {
                if (err) {
                    reject(err);
                    console.log("订单新增失败！");
                } else {
                    console.log("订单新增成功！");
                }
            });
    });
}
addOrder.post('/addOrder', (req, res) => {
    if (req.body.values) {
        const result = query(req.body.values);
        res.send({
            code: 200, // 0 表示处理成功，1 表示处理失败
            message: 'post 请求成功！', // 状态的描述
            result: result, // 需要响应给客户端的数据
        });
    }else{
        console.log(req.body.values);
    }
});
module.exports = addOrder
